

stim_type_ms=round(10*aux_data(1,:))/10;
stim_type_fr=zeros(length(frame_times),1);

for ind=1:length(frame_times)
    stim_type_fr(ind)=stim_type_ms(frame_times(ind));
end

direction_names=rem([0:45:315]+270,360);
direction_codes=[1.5:0.5:5];

for knd=1:length(ROIs)
    for ind=1:length(direction_codes)
        df=[0;diff(ROIs(knd).activity)];
        df(df<0)=0;
        dir_resp(knd,ind)=mean(ROIs(knd).activity(stim_type_fr==direction_codes(ind)));
        dir_dr(knd,ind)=mean(df(stim_type_fr==direction_codes(ind)));
    end
end

velP=diff(aux_data(2,:));
velP(velP>5)=velP(velP>5)-10;
velP(velP<-5)=velP(velP<-5)+10;
velP=ftfil(velP,1000,0,10);
velP=[0 velP];
for ind=1:length(direction_codes)
    act_during_dir(ind)=mean(abs(velP(stim_type_ms==direction_codes(ind))));
end

resp_cells_ind=(max(dir_resp,[],2)-min(dir_resp,[],2))./mean(dir_resp,2)>0.1;

[~,pref_dir]=max(dir_resp,[],2);

figure;
subplot(311)
hist(direction_names(pref_dir(resp_cells_ind)),sort(direction_names))
title(['Neuron no. ' num2str(sel_roi)]);

subplot(312)
plot(direction_names,(dir_resp(sel_roi,:)-min(dir_resp(sel_roi,:)))/(max(dir_resp(sel_roi,:))-min(dir_resp(sel_roi,:))),'o','linewidth',3)
hold on
plot(direction_names,(act_during_dir-min(act_during_dir))/(max(act_during_dir)-min(act_during_dir)),'or')
set(gca,'xtick',sort(direction_names));


% 
% figure;
% axes('position',[0.1 0 0.9 0.5])
% plot(mean(dir_resp))
% axis tight
% 
% axes('position',[0.1 0.5 0.9 0.5])
% plot(mean(dir_dr),'r')
% axis tight

dir_map=-50*ones(size(template));
for ind=1:length(ROIs)
    if resp_cells_ind(ind)
        dir_map(ROIs(ind).indices)=direction_names(pref_dir(ind));
    end
end
subplot(313)
imagesc(dir_map);
colorbar